<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="sys" tagdir="/WEB-INF/tags/sys"%>
<%@taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>

<jsp:include page="editOperator.jsp" />

<script>
	function changeOperatorStatus(id, operate, title, status){
		confirm('确认'+operate+title+'?', function(result){
        	$.ajax({
	 	 	    type:"POST",
	 	 	    url:"${contextPath}/sys/operator/status.action",
				dataType:"json",
				contentType:"application/json; charset=utf-8",
				data:'{"id":"'+id+'","status":"'+status+'"}',
	 	 	    success:function(data){
	 	 	    	grid_operator.reload();
	 	 	    	toastr.success(operate+title+'成功！');  			 	 	    		
	 	 	    }
	 	 	}); 			                
	    });
	 }

	function enableOperator(id, title){
		changeOperatorStatus(id, "启用", title, 0);
	}

	function disableOperator(id, title){
		changeOperatorStatus(id, "禁用", title, -1);
	}

	function deleteOperator(id, title){
		changeOperatorStatus(id, "删除", title, -2);
	}

 	var passwordTitle;
 	function editPassword(id,title){
 		$("#operatorPasswordEditorForm").reset();
 		
 		passwordTitle = title;
 		$("#editFormPasswordId").val(id);
 	 	$("#passwordDialogTitle").text("修改("+title+")密码!");
    	$('#myModalPassword').modal('show');

    	$('#myModalPassword').off('hide.bs.modal');
    	$('#myModalPassword').on('hide.bs.modal', function () {
    		operatorPasswordEditorFormValidator.resetForm();
    	});
 	}

 	function updatePassword(){
		if(operatorPasswordEditorFormValidator.form()){
			$('#myModalPassword').modal('hide');
	 		confirm('确认更新'+passwordTitle+'密码?', function(){
	            	$.ajax({
			 	 	    type:"GET",
			 	 	    async:false,
			 	 	    url:"${contextPath}/sys/operator/password/"+$("#editFormPasswordId").val()+"/"+$("#editFormPasswordFirst").val()+".action",
			 	 	    success:function(data){
				 	 	    if(data.result == 0){
				 	 	    	$.ajax({
						 	 	    type:"POST",
						 	 	    url:"${contextPath}/sys/operator/password.action",
									dataType:"json",
									contentType:"application/json; charset=utf-8",
									data:$('form#operatorPasswordEditorForm').serializeJSON(),
						 	 	    success:function(data){
						 	 	    	grid_operator.reload();
						 	 	    	toastr.success('更新'+passwordTitle+'密码成功！');  			 	 	    		
						 	 	    }
						 	 	});
						 	}else{
				 	 	    	alert('请输入和原密码不一样的密码！');  			 	 	    		
						 	}
			 	 	    }
			 	 	});
	 			},function(){                
	            	$('#myModalPassword').modal('show');
			    }
	        );
		}
	 }
</script>
<webui:panel id="operatorSearch">
	<webui:form method="get" styleClass="form-horizontal" 
		name="operatorSearchForm" id="operatorSearchForm"
		action="${contextPath}/rest/operators/search/search?sort=id,desc">
		<webui:input label="名称" styleClass="col-sm-6" labelClass="col-sm-3"
			inputClass="col-sm-9" type="text" name="loginName">
		</webui:input>
		<webui:button label="" type="button" styleClass="col-sm-4" 
			labelClass="col-sm-8" inputClass="col-sm-4" buttonClass="btn-primary"
			onclick="createOperator()">添加账号
		</webui:button>
		<webui:button label="" type="button" styleClass="col-sm-2" 
			labelClass="col-sm-7" inputClass="col-sm-5" buttonClass="btn-primary"
			onclick="grid_operator.filter()">查询
		</webui:button>
	</webui:form>
	
	<webui:table id="grid_operator" dataUrl="${contextPath}/rest/operators/search/search?sort=id,desc"
	datas="_embedded.operators" editorForm="#operatorEditor" searchForm="#operatorSearch">
		<webui:column editor="tableCheck" format="row.id"></webui:column>
		<webui:column field="loginName" searchable="true" title="登录名">{{row.loginName}}</webui:column>
		<webui:column field="name" title="姓名">{{row.name }}</webui:column>
		<webui:column field="mobile" title="手机">{{row.mobile}}</webui:column>
		<webui:column field="email" title="邮箱">{{row.email}}</webui:column>
		<webui:column field="status"  title="状态">{{row.status==0?'启用':'禁用'}}</webui:column>
		<webui:column style="text-align:rigth"> 
			<a class="btn btn-xs btn-primary" v-bind:href="'javascript:editOperator('+row.id+')'">修改信息</a>	
			<a class="btn btn-xs btn-primary" v-bind:href="'javascript:editPassword('+row.id+',\''+row.name+'\')'">修改密码</a> 
			<a v-if="row.status==0" class="btn btn-xs btn-warning" v-bind:href="'javascript:disableOperator('+row.id+',\''+row.name+'\')'">禁用</a>	
			<a v-if="row.status==-1" class="btn btn-xs btn-warning" v-bind:href="'javascript:enableOperator('+row.id+',\''+row.name+'\')'">启用</a>	
			<a v-if="row.status==-1" class="btn btn-xs btn-danger" v-bind:href="'javascript:deleteOperator('+row.id+',\''+row.name+'\')'">删除</a>	
		</webui:column>
	</webui:table>
</webui:panel>

<div id="myModalPassword" class="modal fade" role="dialog"   aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content animated bounceInRight">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">
					<span aria-hidden="true">×</span><span class="sr-only">Close</span>
				</button>
				<h4 id="passwordDialogTitle" class="modal-title">密码修改</h4>
			</div>
			<div class="modal-body">
				<webui:form method="get" styleClass="form-horizontal" id="operatorPasswordEditorForm">
					<input type="hidden" value="" name="id" id="editFormPasswordId" />
					<webui:input label="密码" labelClass="col-sm-2"
						inputClass="col-sm-10" placeholder="请输入6-12个字符" type="password"
						name="passwordFirst" id="editFormPasswordFirst" split="true">
						<webui:validator required="true"/>
					</webui:input>
					<webui:input label="确认密码" labelClass="col-sm-2"
						inputClass="col-sm-10" placeholder="请输入6-12个字符" type="password"
						name="password" split="true">
						<webui:validator required="true" equalTo="'#editFormPasswordFirst'">{equalTo:'两次输入的密码不一致'}</webui:validator>
					</webui:input>
				</webui:form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-white" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary" onclick="updatePassword()">更新</button>
			</div>
		</div>
	</div>
</div>